package com.szqhyxj.youxiangju.biz.receipt.record.fragment.Online;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.szqhyxj.youxiangju.BasePromptFragment;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.receipt.record.OrderCashDetailVO;
import com.szqhyxj.youxiangju.biz.receipt.record.ReceiptRecordAdapter;
import com.szqhyxj.youxiangju.biz.receipt.record.RowsItem;
import com.szqhyxj.youxiangju.biz.receipt.record.detail.quick.QuickReceiptDetailsActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by leung on 2018/11/26.
 */

public class OnlineReceiptRecordFragment extends BasePromptFragment implements OnlineReceiptContract.View, ReceiptRecordAdapter.ItemClickListener {

    List<RowsItem> lists = new ArrayList<>();
    ReceiptRecordAdapter adapter;
    @BindView(R.id.receiptdata)
    RecyclerView receiptdata;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    Unbinder unbinder;
    OnlineReceiptPresenter presenter;
    @BindView(R.id.none)
    LinearLayout none;
    private LinearLayoutManager mLinearLayoutManager;

    public static OnlineReceiptRecordFragment newInstance() {
        Bundle args = new Bundle();
        OnlineReceiptRecordFragment fragment = new OnlineReceiptRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void start() {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            Log.e("开始2222", "开始2222");
            if (presenter == null) {
                presenter = new OnlineReceiptPresenter(this);
                presenter.init();
            } else {
                presenter.init();
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_online_receipt_record;
    }

    @Override
    public void showRecordData(List<RowsItem> list) {
        if (list.size()==0){
            none.setVisibility(View.VISIBLE);
            swipeLayout.setVisibility(View.GONE);
        }else{
            none.setVisibility(View.GONE);
            swipeLayout.setVisibility(View.VISIBLE);
            lists.clear();
            lists.addAll(list);
            adapter = new ReceiptRecordAdapter(getActivity(), lists, this);
            mLinearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);

            if (list.size() < 10) {
                adapter.changeMoreStatus(adapter.NO_LOAD_MORE);
                adapter.notifyDataSetChanged();
            }

            //添加动画
            receiptdata.setItemAnimator(new DefaultItemAnimator());

            //添加分割线
            //  receiptdata.addItemDecoration(new RefreshItemDecoration(this, RefreshItemDecoration.VERTICAL_LIST));

            receiptdata.setLayoutManager(mLinearLayoutManager);
            receiptdata.setAdapter(adapter);
        }

    }

    @Override
    public void showLoad(List<RowsItem> list) {
        List<RowsItem> footerDatas = new ArrayList<RowsItem>();

        adapter.AddFooterItem(list);
        if (list.size() == 10) {
            //设置回到上拉加载更多
            adapter.changeMoreStatus(adapter.PULLUP_LOAD_MORE);
        } else {
            //没有加载更多了
            adapter.changeMoreStatus(adapter.NO_LOAD_MORE);
        }
    }

    @Override
    public void showNodata() {
        adapter.changeMoreStatus(adapter.NO_LOAD_MORE);

    }

    @Override
    public void enshow() {
        adapter.changeMoreStatus(adapter.NO_LOAD_MORE);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClickItem(OrderCashDetailVO poistionvo, String data) {
        QuickReceiptDetailsActivity.start(getActivity(), poistionvo, data);
    }

    @Override
    public void onResume() {

        super.onResume();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        initListener();

        return rootView;
    }

    private void initListener() {

        initLoadMoreListener();
        initPullRefresh();

    }

    private void initLoadMoreListener() {

        receiptdata.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                //判断RecyclerView的状态 是空闲时，同时，是最后一个可见的ITEM时才加载
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) {

                    //设置正在加载更多
                    adapter.changeMoreStatus(adapter.LOADING_MORE);

                    //改为网络请求
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            presenter.load();

                        }
                    }, 3000);


                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                //最后一个可见的ITEM
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });

    }


    private void initPullRefresh() {
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {


                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // presenter.init();
                        //刷新完成
                        swipeLayout.setRefreshing(false);

                    }

                }, 3000);

            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
